package com.bw.lyc.mvp.view;

import android.graphics.Color;
import android.view.View;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.bw.lyc.R;
import com.bw.lyc.mvp.base.view.BaseActivity;
import com.bw.lyc.mvp.contract.FrameContact;
import com.bw.lyc.mvp.di.DaggerFrameComponent;
import com.bw.lyc.mvp.di.FrameModules;
import com.bw.lyc.mvp.presenter.FramePresenter;
import com.bw.lyc.mvp.view.live.LiveFragment;
import com.bw.lyc.mvp.view.mine.MineFragment;
import com.bw.lyc.mvp.view.news.NewsFragment;
import com.bw.lyc.mvp.view.people.PeopleFragment;
import com.bw.lyc.mvp.view.video.VideoFragment;
import com.bw.lyc.widget.BottomButton;

import java.util.Map;

import constant.DownLoadBy;
import model.UpdateConfig;
import update.UpdateAppUtils;

public class FrameActivity extends BaseActivity<FramePresenter> implements BottomButton.SelectListener, FrameContact.IFrameView {


    private NewsFragment newsFragment;
    private PeopleFragment peopleFragment;
    private VideoFragment videoFragment;
    private LiveFragment liveFragment;
    private MineFragment mineFragment;
    private BottomButton paperNews;
    private BottomButton paperPeople;
    private BottomButton paperVideo;
    private BottomButton paperLive;
    private BottomButton paperMine;


    @Override
    public View addStatusView() {
        return f(R.id.frame_relative);
    }


    @Override
    public int bindLayout() {
        return R.layout.activity_frame;
    }

    @Override
    public void initView() {
        paperNews = (BottomButton) findViewById(R.id.paper_news);
        paperPeople = (BottomButton) findViewById(R.id.paper_people);
        paperVideo = (BottomButton) findViewById(R.id.paper_video);
        paperLive = (BottomButton) findViewById(R.id.paper_live);
        paperMine = (BottomButton) findViewById(R.id.paper_mine);

        paperNews.setContent("新闻").setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.mipmap.tu1_f)
                .setSelImg(R.mipmap.tu1_t)
                .setListener(FrameActivity.this::onSelect)
                .setShowPoint(false);

        paperPeople.setContent("人民号").setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.mipmap.tu2_f)
                .setSelImg(R.mipmap.tu2_t)
                .setListener(FrameActivity.this::onSelect)
                .setShowPoint(false);

        paperVideo.setContent("视频").setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.mipmap.tu3_f)
                .setSelImg(R.mipmap.tu3_t)
                .setListener(FrameActivity.this::onSelect)
                .setShowPoint(false);

        paperLive.setContent("直播").setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.mipmap.tu4_f)
                .setSelImg(R.mipmap.tu4_t)
                .setListener(FrameActivity.this::onSelect)
                .setShowPoint(false);

        paperMine.setContent("我的").setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.mipmap.tu5_f)
                .setSelImg(R.mipmap.tu5_t)
                .setListener(FrameActivity.this::onSelect)
                .setShowPoint(false);
        newsFragment = new NewsFragment();
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.paper_ll, newsFragment);
        fragmentTransaction.commit();
        setStatusBarColor(Color.WHITE);


    }


    @Override
    public void onSelect(int id) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        if (newsFragment != null) {
            fragmentTransaction.hide(newsFragment);
        }
        if (peopleFragment != null) {
            fragmentTransaction.hide(peopleFragment);
        }
        if (videoFragment != null) {
            fragmentTransaction.hide(videoFragment);
        }
        if (liveFragment != null) {
            fragmentTransaction.hide(liveFragment);
        }
        if (mineFragment != null) {
            fragmentTransaction.hide(mineFragment);
        }
        if (id == R.id.paper_news) {
            paperPeople.clearSelect();
            paperVideo.clearSelect();
            paperLive.clearSelect();
            paperMine.clearSelect();
            setStatusBarColor(Color.WHITE);
            fragmentTransaction.show(newsFragment);
        } else if (id == R.id.paper_people) {
            paperNews.clearSelect();
            paperVideo.clearSelect();
            paperLive.clearSelect();
            paperMine.clearSelect();
            setStatusBarColor(Color.WHITE);
            if (peopleFragment == null) {
                peopleFragment = new PeopleFragment();
                fragmentTransaction.add(R.id.paper_ll, peopleFragment);
            } else {
                fragmentTransaction.show(peopleFragment);
            }
        } else if (id == R.id.paper_video) {
            paperNews.clearSelect();
            paperPeople.clearSelect();
            paperLive.clearSelect();
            paperMine.clearSelect();
            setStatusBarColor(1);
            if (videoFragment == null) {
                videoFragment = new VideoFragment();
                fragmentTransaction.add(R.id.paper_ll, videoFragment);
            } else {
                fragmentTransaction.show(videoFragment);
            }
        } else if (id == R.id.paper_live) {
            paperNews.clearSelect();
            paperPeople.clearSelect();
            paperVideo.clearSelect();
            paperMine.clearSelect();
            setStatusBarColor(Color.WHITE);
            if (liveFragment == null) {
                liveFragment = new LiveFragment();
                fragmentTransaction.add(R.id.paper_ll, liveFragment);
            } else {
                fragmentTransaction.show(liveFragment);
            }
        } else if (id == R.id.paper_mine) {
            paperNews.clearSelect();
            paperVideo.clearSelect();
            paperLive.clearSelect();
            paperPeople.clearSelect();
            setStatusBarColor(Color.RED);
            if (mineFragment == null) {
                mineFragment = new MineFragment();
                fragmentTransaction.add(R.id.paper_ll, mineFragment);
            } else {
                fragmentTransaction.show(mineFragment);
            }
        }
        fragmentTransaction.commit();
    }

    @Override
    public void inject() {
        DaggerFrameComponent.builder().frameModules(
                new FrameModules(this)
        ).build().injectFrame(this);
    }

    @Override
    public void initData() {
        UpdateConfig config=new UpdateConfig(true,true,
                false,false,false
                , "","apk.1.1"
                , DownLoadBy.APP
                ,false,true
                ,0
                ,false,true,
                "",2);
        UpdateAppUtils.getInstance().apkUrl("upDataEntity.getUrl()")
                .updateTitle("更新版本")
                .updateContent("一日之计在于晨.")
                .updateConfig(config)
                .update();
    }


    @Override
    public Void refresh(Map<String, Object> resultmap) {
        return null;
    }
}
